<?php

namespace App\Admin\Controllers;

use App\Models\Collection;
use App\Models\User;
use App\Models\Video;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CollectionContollrer extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '收藏';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Collection());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('video_name', __('影视名称'))->display(function ($video_name){
            return $video_name['name'];
        });
        $grid->column('user_nickname', __('用户昵称'))->display(function($user_nickname){
            return $user_nickname['nickname'];
        });
        $grid->column('created_at', __('收藏时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Collection::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('id', __('Id'));
        $show->field('video_name', __('影视id'))->as(function($video_name){
            return $video_name['name'];
        });
        $show->field('user_nickname', __('用户id'))->as(function ($user_nickname){
            return $user_nickname['nickname'];
        });
        $show->field('created_at', __('收藏时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Collection());
        $form->select('video_id', __('影视id'))->options(function ($id){
            $video = Video::find($id);
            if ($video) {
                return [$video->id => $video->name];
            }
        })->ajax('/api/admin_videolist')->rules('required');
        $form->select('user_id', __('用户id'))->options(function ($id){
            $video = User::find($id);
            if ($video) {
                return [$video->id => $video->nickname];
            }
        })->ajax('/api/admin_user_list')->rules('required');
        return $form;
    }
}
